import { _decorator, Component, Node, tween } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('SheepMove')
export class SheepMove extends Component {

    @property([Node])
    movePointsTrans: Node[] = []

    start() {
        if (this.movePointsTrans.length > 0) {
            this.moveAlongPath()
        }
    }

    moveAlongPath() {
        let points = this.movePointsTrans.map(point => point.getPosition())
        let currentIndex = 0

        const moveToNext = () => {
            let targetPos = points[currentIndex]
            currentIndex = (currentIndex + 1) % points.length

            tween(this.node)
                .to(1, {position: targetPos}, {easing: 'sineInOut' })
                .call(moveToNext)
                .start()
        }
        moveToNext()
    }

    update(deltaTime: number) {
        /**
         * 小羊面朝左边，如果在x轴上方，那么反转小羊
         */
        if (this.node.position.y > 0) {
            this.node.setScale(-1, 1)
        } else {
            this.node.setScale(1, 1)
        }
    }
}

